import {useRapier, RigidBody} from '@react-three/rapier';
import {useFrame} from '@react-three/fiber';
import {useKeyboardControls} from '@react-three/drei';
import {useEffect, useRef, useState} from 'react';
import * as THREE from 'three';


const Player = (props) => {

    const [postionOffset, setPostionOffset] = useState(new THREE.Vector3(10, 10, 10))
    const [targetOffset, setTargetOffset] = useState(new THREE.Vector3())

    const playerRef = useRef()
    const [sub, get] = useKeyboardControls()
    // const { raaapier, world} = useRapier()
    // const repierWorld = world.raw()

    const jump = () => {
        const origin = playerRef.current.translation()
        // origin.y -= 0.31
        // const direction = new THREE.Vector3(0, -1, 0)
        // const ray = new rapier.Ray(origin, direction)
        // const hit = repierWorld.castRay(ray, 10, true)
        // if (hit.toi < 0.15) {
        //     playerRef.current.applyImpulse(new THREE.Vector3(0, 0.5, 0))
        // }
        if (origin.y < 0.3)
            playerRef.current.applyImpulse(new THREE.Vector3(0, 0.5, 0))
    }

    useEffect(() => {
        return sub(
          (state) => state.jump,
          (pressed) => {
              if (pressed) jump()
          }
        )
    }, [])

    useFrame((state, delta) => {
        const bodyPosition = playerRef.current.translation()
        console.log('bodyPosition', bodyPosition);
        // if (bodyPosition.y <= 0) {
            // 重置位置
            // playerRef.current.position = new THREE.Vector3(0, 0.5, 0)
            // playerRef.current?.setLinearVelocity(new THREE.Vector3(0, 0, 0))
            // playerRef.current?.setAngularVelocity(new THREE.Vector3(0, 0, 0))
        // }

        const {back, left, right, forward} = get()
        // Fetch fresh data from store

        const impulse = new THREE.Vector3(0.001, 0, 0)
        const torque = new THREE.Vector3(0, 0, 0)

        const impulseStrength = 0.6 * delta
        const torqueStrength = 0.2 * delta

        if (back) {
            impulse.z += impulseStrength
            torque.x += torqueStrength
        }
        if (left) {
            impulse.x -= impulseStrength
            torque.z += torqueStrength
        }
        if (right) {
            impulse.x += impulseStrength
            torque.z -= torqueStrength
        }
        if (forward) {
            impulse.z -= impulseStrength
            torque.x -= torqueStrength
        }

        playerRef.current.applyImpulse(impulse)
        playerRef.current.applyTorqueImpulse(torque)

        // console.log('bodyPosition', bodyPosition);
        const cameraPosition = new THREE.Vector3(0, 0, 0)
        cameraPosition.copy(bodyPosition)
        cameraPosition.z += 2.25
        cameraPosition.y += 0.65

        postionOffset.lerp(cameraPosition, 5 * delta)

        state.camera.position.copy(postionOffset)

        const cameraTarget = new THREE.Vector3(0, 0, 0)
        cameraTarget.copy(bodyPosition)
        // props.camera.lookAt(cameraTarget)
        cameraTarget.y += 0.25
        cameraTarget.z += 0.25

        targetOffset.lerp(cameraTarget, 5 * delta)

        state.camera.lookAt(cameraTarget)

    })

    return <RigidBody ref={playerRef} colliders={'ball'} restitution={0.2} friction={1}>
        <mesh receiveShadow castShadow>
            <icosahedronGeometry  args={[0.3, 1]} />
            <meshStandardMaterial flatShading color="mediumpurple" />
        </mesh>
    </RigidBody>

}

export default Player;
